package com.dudu.log.voiceLog.action;

import java.util.List;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.apache.commons.lang.StringUtils;

import com.dudu.bean.UserBean;
import com.dudu.log.voiceLog.service.IVoiceService;
import com.dudu.log.voiceLog.service.impl.VoiceServiceImpl;

import core.CoreService;

public class VoiceAction extends CoreService implements IVoiceAction {

	// 当前登陆用户
	private final String KEY_CURRENT_USER = "key_current_user";
	// 状态查询service
	private IVoiceService voiceService = new VoiceServiceImpl();

	@Override
	public String queryAllVoiceList() {
		JSONObject object = new JSONObject();
		JSONArray jsonArray = new JSONArray();
		// 获取当前登录用户信息
		UserBean currentUser = (UserBean) this.getSession().getAttribute(KEY_CURRENT_USER);
		if (currentUser != null) {
			// 获取页面分页用信息
			String pageSql = this.getPageSql();
			// 获取查询参数信息
			String paramSql = this.convertParams();
			// 总数量
			int totalAcount = this.voiceService.getTotalAcount(paramSql);
			List<JSONObject> list = this.voiceService.queryAll(paramSql, pageSql);
			jsonArray = JSONArray.fromObject(list);

			JSONObject dataobject = new JSONObject();
			object.put("flag", "000000");
			object.put("desc", "success");
			dataobject.put("totalItemNum", totalAcount);
			dataobject.put("items", jsonArray);
			object.put("data", dataobject);
		} else {
			object.put("flag", "100000");
		}
		return returnData(object.toString());
	}

	/**
	 * 重发（单个）
	 * 1、调用通道失败，再次向通道提交; 2、调用通道成功，通道发起失败没有产生计费话单，再次向通道提交。
	 */
	@Override
	public String resendVoiceById() {
		JSONObject object = new JSONObject();
		// 获取当前登录用户信息
		UserBean currentUser = (UserBean) this.getSession().getAttribute(KEY_CURRENT_USER);
		if (currentUser != null) {
			String id = this.getRequest().getParameter("id");
			if(StringUtils.isNotBlank(id)){
				int count = voiceService.resendVoiceById(id);
				if(count != -1){
					object.put("flag", "000000");
				}else{
					object.put("flag", "100000");
				}
			}else{
				object.put("flag", "100000");
			}
		} else {
			object.put("flag", "100000");
		}
		return returnData(object.toString());
	}
	
	/**
	 * 重发（批量）
	 * 1、调用通道失败，再次向通道提交; 2、调用通道成功，通道发起失败没有产生计费话单，再次向通道提交。
	 */
	@Override
	public String resendVoiceByParam() {
		JSONObject object = new JSONObject();
		// 获取当前登录用户信息
		UserBean currentUser = (UserBean) this.getSession().getAttribute(KEY_CURRENT_USER);
		if (currentUser != null) {
			// 获取查询参数信息
			String paramSql = this.convertParams();
			if(StringUtils.isNotBlank(paramSql)){
				int count = voiceService.resendVoiceByParam(paramSql);
				if(count != -1){
					object.put("flag", "000000");
					object.put("desc", "success");
					object.put("count", count);
				}else{
					object.put("flag", "100000");
				}
			}else{
				object.put("flag", "100000");
			}
		} else {
			object.put("flag", "100000");
		}
		return returnData(object.toString());
	}


}
